﻿using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace SellAndBuy.Web.Controllers.ActionResults
{
     public static class ImageResultHelper
    {
       // public static string Image<T>(this HtmlHelper helper,
       //     Expression<Action<T>> action, int? width, int? height)
       //     where T : Controller
       // { ... }
     
       // public static string Image<T>(this HtmlHelper helper,
       //     Expression<Action<T>> action, int? width, int? height, string alt)
       //   where T : Controller
       //{ ... }
    
       //public static string Image<T>(this HtmlHelper helper,
       //    Expression<Action<T>> action, string id, string alt, string @class)
       //    where T : Controller
       //{ ... }
    
       //public static string Image<T>(this HtmlHelper helper,
       //    Expression<Action<T>> action, int? width, int? height, string alt, string @class)
       //    where T : Controller
       //{ ... }
    
       //public static string Image<T>(this HtmlHelper helper,
       //    Expression<Action<T>> action, string alt, string @class)
       //    where T : Controller
       //{ ... }
    
       //public static string Image<T>(this HtmlHelper helper,
       //    Expression<Action<T>> action, string id, int? width, int? height,
       //    string alt, string @class)
       //    where T : Controller
       //{ ... }
    
       public static string Image<T>(this HtmlHelper helper,
           Expression<Action<T>> action, string id, int? width, int? height,
           string alt, string @class, object attrs)
           where T : Controller
       {
           //string url = helper.BuildUrlFromExpression.BuildUrlFromExpression<T>(action);
           var expression = action.Body as MethodCallExpression;
           var actionMethodName = string.Empty;
           if (expression != null)
           {
               actionMethodName = expression.Method.Name;
           }
           var url = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection).Action(actionMethodName, typeof(T).Name.Remove(typeof(T).Name.IndexOf("Controller")));         

           var sb = new StringBuilder();
    
           if (!string.IsNullOrEmpty(url))
           {
               sb.AppendFormat("src=\"{0}\" ", url);
           }
           if (!string.IsNullOrEmpty(id))
           {
               sb.AppendFormat("id=\"{0}\" ", id);
           }
          if (width != null)
           {
               sb.AppendFormat("width=\"{0}\" ", width.Value);
           }
           if (height != null)
           {
               sb.AppendFormat("height=\"{0}\" ", height.Value);
           }
           if (!string.IsNullOrEmpty(alt))
           {
               sb.AppendFormat("alt=\"{0}\" ", alt);
           }
           if (!string.IsNullOrEmpty(@class))
           {
               sb.AppendFormat("class=\"{0}\" ", @class);
           }
    
           AddAttributes(sb, attrs);
    
           return string.Format("<img {0} />", sb);
       }
    
       private static void AddAttributes(StringBuilder sb, object attrs)
       {
           var routeValues = new RouteValueDictionary(attrs);
           foreach (var kv in routeValues)
           {
               sb.AppendFormat("{0}=\"{1}\" ", kv.Key, kv.Value);
           }
       }
   }
}